
package com.sx.core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;



/**
 * @author LQZ
 * @date 2009-12-19
 * @version 1.0
 * <H3>数据库打开关闭连接</H3>
 *
 */
public class DBConfig {

	private String coonectionflag = "";//1为JNDI连接 0为JDBC连接
	private String driver = "";
	private String url = "";
	private String username = "";
	private String pwd = "";
	private String jndiname = "";
	private Connection con;
	public DBConfig() {
		init();
	}
	private void init()
	{
		driver = StaticValue.getValue("name");
		url = StaticValue.getValue("url");
		username = StaticValue.getValue("username");
		pwd = StaticValue.getValue("pwd");
		jndiname = StaticValue.getValue("jndiname");
		coonectionflag = StaticValue.getValue("coonectionflag");
	}
	/**
	 *<pre>私有方法 获取JDBC连接方法</pre> 
	 * @return Connection
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	public Connection getJdbcConnection(){
		try {
//			System.out.println("执行�? JdbcConnection");
			Class.forName(driver);
			con = DriverManager.getConnection(url,username,pwd);
		}catch (SQLException e) {
				e.printStackTrace();
		}catch (ClassNotFoundException e) {
				e.printStackTrace();
		}
		return con;
	}
	/**
	 *<pre>私有方法 获取JNDI连接方法</pre> 
	 * @return Connection
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	private Connection getJndiConnection(){
		Context ctx = null;
		try {
//			System.out.println("执行�? JNDI Connection");
			ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup(jndiname);
			con = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return con;
	}
	/**
	 *<pre>公有方法 获取Connection连接方法</pre> 
	 * @return Connection
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	public Connection getConnection()
	{
		if (this.coonectionflag.equals("1")) {
			con = this.getJndiConnection();
		} else {
			con = this.getJdbcConnection();
		}
		return con;
	}
	/**
	 *<pre>公有方法 关闭PreparedStatement数据库连接方�?</pre> 
	 * @return void
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	public void close(ResultSet rs,PreparedStatement pst,Connection con)
	{
		try {
			if (rs!=null) {rs.close();}
			rs = null;
		} catch (Exception e) {
			rs = null;
			e.printStackTrace();
		}
		try {
			if (pst!=null) {pst.close();}
			pst = null;
		} catch (Exception e) {
			pst = null;
			e.printStackTrace();
		}
		try {
			if(con!=null){con.close();}
			con = null;
		} catch (Exception e) {
			con = null;
			e.printStackTrace();
		}
	}
	/**
	 *<pre>公有方法 关闭Statement数据库连接方�?</pre> 
	 * @return void
	 * @author LQZ 
	 * @date 2009-12-19
	 * */
	public void close(ResultSet rs,Statement st,Connection con)
	{
		try {
			if (rs!=null) {rs.close();}
			rs = null;
		} catch (Exception e) {
			rs = null;
			e.printStackTrace();
		}
		try {
			if (st!=null) {st.close();}
			st = null;
		} catch (Exception e) {
			st = null;
			e.printStackTrace();
		}
		try {
			if(con!=null){con.close();}
			con = null;
		} catch (Exception e) {
			con = null;
			e.printStackTrace();
		}
	}
}
